package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.businessException.AddException;
import com.itheima.businessException.DeleteException;
import com.itheima.businessException.FindException;
import com.itheima.businessException.UpdateException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemSevice;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiangTianQi
 * @describe 检查项中的controller层
 * @date 2020/11/8 18:36
 */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemSevice checkItemSevice;

    /**
     * 添加
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    //ajax请求发过来的是json字符串，不是表单提交所以要加上@requstbody
    public Result add(@RequestBody CheckItem checkItem) throws AddException {
        boolean flag = false;

            flag = checkItemSevice.add(checkItem);
            return new Result(flag,MessageConstant.ADD_CHECKITEM_SUCCESS);

    }

    @PostMapping("/getByConditions")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean)throws FindException {

        String queryString = queryPageBean.getQueryString();
        System.out.println(queryString);
        PageResult pageResult = null;

            pageResult = checkItemSevice.getByConditions(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);

    }

    @GetMapping("/delete")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id) throws DeleteException {
        boolean flag = false;

            flag = checkItemSevice.delete(id);
            if (flag){
                return  new Result(flag,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }else{
                return  new Result(flag,MessageConstant.DELETE_CHECKITEM_FAIL);
            }

    }

    @GetMapping("/toUpdate")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_UPDATE')")
    public Result toUpdate(Integer id) throws FindException {
            CheckItem checkItem = checkItemSevice.get(id);
            return  new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkItem);

    }

    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_UPDATE')")
    //ajax请求发过来的是json字符串，不是表单提交所以要加上@requstbody
    public Result update(@RequestBody CheckItem checkItem)throws UpdateException {
        boolean flag = false;

            flag = checkItemSevice.update(checkItem);
            return new Result(flag,MessageConstant.EDIT_CHECKITEM_SUCCESS);

    }

    @GetMapping("/getAll")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result getAll() throws FindException{

            List<CheckItem> all = checkItemSevice.getAll();
            return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,all);

    }

}
